import 'dart:ui';

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: RectDemo(),
    );
  }
}

class RectDemo extends StatefulWidget {
  @override
  _RectDemoState createState() => _RectDemoState();
}

class _RectDemoState extends State<RectDemo> {
  Offset? _clickOffset;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('绘制专题')),
      body: Center(
        child: GestureDetector(
          onPanUpdate: (DragUpdateDetails details) {
            //更新手指的位置
            _clickOffset = details.localPosition;
            setState(() {});
          },
          child: Container(
            width: 500,
            height: 500,
            color: Colors.blue.shade100,
            child: CustomPaint(
              size: Size(500, 500),
              //画布
              //绘制工作
              painter: RectPainter(),
            ),
          ),
        ),
      ),
    );
  }
}

class RectPainter extends CustomPainter {
  Paint _paint = Paint()
    ..color = Colors.red
    ..strokeWidth = 3;
  @override
  void paint(Canvas canvas, Size size) {
    //创建矩形
    Rect rect = Rect.fromLTRB(10, 10, 200, 200);

    //前两个点 是矩形 左上角的坐标  （100, 150）
    Rect rect1 = Rect.fromLTWH(100, 150, 100, 150);

    //通过圆形创建外切下方形
    Rect rect2 = Rect.fromCircle(center: Offset(100, 100), radius: 100);

    //根据中心与尺寸来创建
    Rect rect3 = Rect.fromCenter(
      center: Offset(100, 100),
      width: 100,
      height: 50,
    );

    //根据两个对角点来创建矩形
    Rect rect4 = Rect.fromPoints(Offset(100, 100), Offset(200, 200));

    _paint.color = Colors.red;
    //绘制
    canvas.drawRect(rect4, _paint);

    _paint.color = Colors.blue;
    canvas.drawPoints(PointMode.points, [Offset(100, 100)], _paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
